package cn.school.workflow.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.school.common.utils.SysUserUtils;
import cn.school.workflow.dao.VisitApplyDao;
import cn.school.workflow.dao.VisitorInfoDao;
import cn.school.workflow.service.VisitApplyService;
import cn.school.workflow.service.WorkFlowCommonService;
import cn.school.workflow.vo.VisitApplyVO;
import cn.school.workflow.vo.VisitorInfoVO;
import cn.school.workflow.vo.WorkFlowVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class VisitApplyServiceImpl extends ServiceImpl<VisitApplyDao, VisitApplyVO> implements VisitApplyService {

    @Autowired
    private WorkFlowCommonService workFlowCommonService;
    @Autowired
    private VisitorInfoDao visitorInfoDao;

    @Override
    public IPage<VisitApplyVO> findVisitApplyPageList(VisitApplyVO vo) {
        LambdaQueryWrapper<VisitApplyVO> wrapper = new LambdaQueryWrapper<>(vo);
        if(!StringUtils.isEmpty(vo.getApplyNo())){
            wrapper.eq(VisitApplyVO::getApplyNo,vo.getApplyNo());
        }
        wrapper.orderByDesc(VisitApplyVO::getCreateTime);
        return page(new Page<>(vo.getPageNum(), vo.getPageSize()), wrapper);
    }

    @Override
    public boolean cancelVisitApply(WorkFlowVO vo) {
        LambdaQueryWrapper<VisitApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VisitApplyVO::getProcessId,vo.getProcessInstanceId());
        VisitApplyVO applyVO = new VisitApplyVO();
        applyVO.setApplyStatus("2");//撤销流程
        this.baseMapper.update(applyVO,wrapper);
        return workFlowCommonService.cancelApply(vo.getProcessInstanceId(),vo.getApplyDesc());

    }

    @Override
    @Transactional
    public int createVisitApply(VisitApplyVO vo) {
        String username = SysUserUtils.getSysUser().getUsername();

        String applyNo = "VS_"+ DateUtil.format(new Date(),"yyyyMMddHHmmssSSS");
        vo.setApplyNo(applyNo);
        vo.setCreateBy(username);
        vo.setCreateTime(new Date());
        //创建申请单
        this.baseMapper.insert(vo);
        //添加访客
        for(VisitorInfoVO infoVO :vo.getVisitorInfoList()){
            infoVO.setVisitId(vo.getId());
            infoVO.setCreateBy(username);
            infoVO.setCreateTime(new Date());
            visitorInfoDao.insert(infoVO);
        }

        WorkFlowVO flowVO = new WorkFlowVO();
        flowVO.setApplyTitle("访客申请流程");
        flowVO.setApplyID("visitApply");
        flowVO.setBusinessKey(applyNo);
        //创建请假工作流
        String processInstanceId = workFlowCommonService.createProcessInstance(flowVO);
        vo.setProcessId(processInstanceId);
        //更新流程实例ID
        return this.baseMapper.updateById(vo);
    }

    @Override
    public VisitApplyVO findVisitApplyDetail(Long processInstanceId) {
        LambdaQueryWrapper<VisitApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VisitApplyVO::getProcessId,processInstanceId);
        return this.baseMapper.selectOne(wrapper);
    }
}
